﻿using System.Windows;
using System.Windows.Controls;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class JLTABAUDITData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public bool IsAB05 = true;
        public bool IsAB55 = true;
        public bool IsAB45 = true;
        public bool IsAB50 = true;
        string VKAUFKRS = "";
        string VKAUFLFD = "";
        string VKAUFPOS = "";

        SETINTEXDB INTEXDB = new SETINTEXDB();
        string ABPLFD = "X";
        int ABPOS = 1;
        public decimal LDAB05 = 1;
        public decimal LDAB55 = 1;
        public decimal LDAB45 = 1;
        public decimal LDAB50 = 1;
        DataSet SetDispo = new DataSet();
        Queue<DataSet> JobQueue;
        DataSet Set4ITX = new DataSet();
        DataRow dwIntexStd = null;
        bool IsRun = false;
        string PRLO = "", PRLB = "", PRLP = "";
        string time;
        int ABAID = 1;
        public DataSet SetMap;
        public Convert2Intex Convert2Intex;
        public DataSet SetExt = new System.Data.DataSet();
        public DataSet SetJob;
        public SETINTEXDB SetData;
        JLTONAV95Data m_AV95;
        DataTable tbABAUFM = null;
        Dictionary<string, string> pABPAUFLFD = new Dictionary<string, string>();
        BackgroundWorker bgWorker;
        public JLTABAUDITData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            JobQueue = new Queue<DataSet>();
            bgWorker = new BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
            m_AV95 = new JLTONAV95Data();
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] = Left(Password + "                    ", 20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
        public JLTABAUDITData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            Convert2Intex = new Convert2Intex();
            JobQueue = new Queue<DataSet>();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            bgWorker = new BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
        }
        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsRun = false;
        }
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!IsRun)
            {
                IsRun = true;
            }

        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            Helper.GetDate(ref date, ref year, ref month, ref day);
            SetMap = new DataSet();
            DataSet STemp = new System.Data.DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\JLTAV95MAPINTEX.XML");
            STemp.ReadXml(filePath);
            SetMap.Merge(STemp);
            filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
            if (!System.IO.File.Exists(filePath))
            {
                return SReturn;
            }
            SResult.ReadXml(filePath);
            SReturn.Merge(SResult);
            DataRow dwData;
            dwData = INTEXDB.APKOPF.NewRow();
            dwData["APKMANDANT"] = "00";
            SResult = INTEXDBC.QueryAPKOPF(dwData, 1);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet LoadData(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            OracleCommand or1;
            string KRS = "";
            string LFD = "";
            DataRow dwData = null;
            try
            {
                KRS = dwInput["VKPAUFKRS"].ToString();
                LFD = dwInput["VKPAUFLFD"].ToString();

                dwData = INTEXDB.VKKOPF.NewRow();
                dwData["VKKMANDANT"] = "00";
                dwData["VKKAUFKRS"] = KRS;
                dwData["VKKAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryVKKOPF(dwData, 1);
                SReturn.Merge(SResult);
                dwData = INTEXDB.VKPOS.NewRow();
                dwData["VKPMANDANT"] = "00";
                dwData["VKPAUFKRS"] = KRS;
                dwData["VKPAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryVKPOS(dwData, 2);
                SReturn.Merge(SResult);

                dwData = INTEXDB.ICC_ABAX.NewRow();
                dwData["ABPART"] = Left(KRS + "-" + Right(LFD, 4).PadRight(20), 20);
                SResult = INTEXDBC.QueryICC_ABAX(dwData, 2);
                SReturn.Merge(SResult);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    //MessageBox.Show("生产单已经创建完毕，请去生产单处理界面查看"); 
                }
                return SReturn;
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryAPZUORD(dwData, 2);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryAPZUORD(dwData, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryAPZUORD(dwData, 4);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryAPZUORD(dwData, 5);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                DataSet STemp = new System.Data.DataSet();
                if (SReturn.Tables.Contains("APZUORD"))
                {
                    string processed = "";
                    foreach (DataRow rx in SReturn.Tables["APZUORD"].Rows)
                    {
                        string APZANRKRS = rx["APZANRKRS"].ToString();
                        string APZALFDNR = rx["APZALFDNR"].ToString();
                        if (processed.Contains(APZANRKRS + APZALFDNR + ";"))
                            continue;
                        processed += APZANRKRS + APZALFDNR + ";";
                        dwData = INTEXDB.APFOLGE.NewRow();
                        dwData["APFMANDANT"] = rx["APZMANDANT"];
                        dwData["APFANRKRS"] = APZANRKRS;
                        dwData["APFALFDNR"] = APZALFDNR;
                        SResult = INTEXDBC.QueryAPFOLGE(dwData, 4);
                        STemp.Merge(SResult);
                        dwData = INTEXDB.APARTK.NewRow();
                        dwData["APAKMANDANT"] = rx["APZMANDANT"];
                        dwData["APAKANRKRS"] = APZANRKRS;
                        dwData["APAKALFDNR"] = APZALFDNR;
                        SResult = INTEXDBC.QueryAPARTK(dwData, 1);
                        STemp.Merge(SResult);
                    }
                    SReturn.Merge(STemp);
                }
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryWDECK(dwData, 4);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryWASTAMM(dwData, 5);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryWASTKE(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                dwData["WDSA"] = "C";
                SResult = INTEXDBC.QueryWAFARBEN(dwData, 6);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                dwData["WDSA"] = "B";
                SResult = INTEXDBC.QueryWAFARBEN(dwData, 7);
                SReturn.Merge(SResult);

                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryWASTGA(dwData, 4);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryBSSATZ(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryBSGROE(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryBHSATZ(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryBASTGA(dwData, 2);
                SReturn.Merge(SResult);
                STemp = new System.Data.DataSet();
                string yarns = "";
                string Yart = "";
                string Yvar = "";
                foreach (DataRow row in SReturn.Tables["WASTGA"].Rows)
                {
                    dwData = INTEXDB.GSDAT.NewRow();
                    Yart = row["WAGGART"].ToString();
                    Yvar = row["WAGGVAR"].ToString();
                    if (yarns.Contains(Yart + Yvar + ";"))
                    {
                        continue;
                    }
                    yarns += Yart + Yvar + ";";
                    dwData["GSMANDANT"] = row["WAGMANDANT"];
                    dwData["GSART"] = Yart;
                    dwData["GSVAR"] = Yvar;
                    SResult = INTEXDBC.QueryGSDAT(dwData);
                    STemp.Merge(SResult);
                }
                SReturn.Merge(STemp);
                STemp = new System.Data.DataSet();
                foreach (DataRow row in SReturn.Tables["BASTGA"].Rows)
                {
                    Yart = row["BAGGART"].ToString();
                    Yvar = row["BAGGVAR"].ToString();
                    if (yarns.Contains(Yart + Yvar + ";"))
                    {
                        continue;
                    }
                    yarns += Yart + Yvar + ";";
                    dwData = INTEXDB.GSDAT.NewRow();
                    dwData["GSMANDANT"] = row["BAGMANDANT"];
                    dwData["GSART"] = Yart;
                    dwData["GSVAR"] = Yvar;
                    SResult = INTEXDBC.QueryGSDAT(dwData);
                    STemp.Merge(SResult);
                    dwData = INTEXDB.GASTGA.NewRow();
                    dwData["GAGMANDANT"] = row["BAGMANDANT"];
                    dwData["GAGART"] = Yart;
                    SResult = INTEXDBC.QueryGASTGA(dwData, 2);
                    STemp.Merge(SResult);
                }
                SReturn.Merge(STemp);
                foreach (DataRow row in SReturn.Tables["GSDAT"].Rows)
                {
                    dwData = INTEXDB.GASTGA.NewRow();
                    dwData["GAGMANDANT"] = row["GSMANDANT"];
                    dwData["GAGART"] = row["GSART"];
                    SResult = INTEXDBC.QueryGASTGA(dwData, 2);
                    STemp.Merge(SResult);
                }
                SReturn.Merge(STemp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void Combine(DataSet src, double RQTY, string DD)
        {
            DataRow dwABAN = null;
            DataRow dwABPN = null;
            foreach (DataRow row in src.Tables["ICC_ABAX"].Rows)
            {
                dwABPN = row;
            } 
            double GAKG1 = 0;
            GAKG1 = RQTY * 100;
            tbABAUFM = INTEXDB.ICC_ABAX.Clone();
            dwABAN = tbABAUFM.NewRow();
            dwABAN.ItemArray = m_AV95.NewABA().ItemArray;
            dwABAN["ABAID"] = ABAID; ABAID++;
            dwABAN["ABAAUFTYP"] = "N";
            dwABAN["ABAVKAUFTYP"] = "V";
            dwABAN["ABAVKAUFKRS"] = dwABPN["ABAAUFKRS"];
            dwABAN["ABAVKAUFLFD"] = dwABPN["ABAAUFLFD"];
            dwABAN["ABAVKAUFPOS"] = dwABPN["ABAAUFPOS"];
            dwABAN["ABAVKUPOS"] = "01";

            dwABAN["ABPWAKZ"] = dwABPN["ABAWAKZ"];
            dwABAN["ABPART"] = dwABPN["ABAART"];
            dwABAN["ABPVAR"] = dwABPN["ABAVAR"];
            dwABAN["ABPFARBE"] = dwABPN["ABAFARBE"];
            dwABAN["ABAWAKZ"] = dwABPN["ABAWAKZ"];
            dwABAN["ABAART"] = dwABPN["ABAART"];
            dwABAN["ABAVAR"] = dwABPN["ABAVAR"];
            dwABAN["ABAFARBE"] = dwABPN["ABAFARBE"];
            dwABAN["ABASSTKLG"] = 0;
            dwABAN["ABARESMG"] = GAKG1;
            dwABAN["ABAVKENDD"] = dwABPN["ABAVKENDD"];
            dwABAN["ABAVKSTARTD"] = dwABPN["ABAVKSTARTD"];
            dwABAN["ABABASMG"] = 0;
            dwABAN["ABADISPOKZ"] = "B";
            dwABAN["ABALO"] = PRLO;
            dwABAN["ABALB"] = PRLB;
            dwABAN["ABALP"] = PRLP;
            dwABAN["ABAUSER"] = UserID;
            dwABAN["ABADATE"] = date;
            tbABAUFM.Rows.Add(dwABAN);
            m_AV95.tbABAUFM = tbABAUFM;
            //m_AV95.SetData =new SETINTEXDB();
            DataSet SResult = new DataSet();// m_AV95.PlanAB50(SetData, dwABAN, DD);
            DataSet SrcProd = new DataSet();
            SrcProd.Merge(m_AV95.tbABAUFM);
            SrcProd.Merge(SResult);
           
            SResult = new DataSet();
            SResult.Merge(SrcProd);
            string path = @"C:\IntexReport\Data\JLTAV95";
            try
            {
                SResult.Tables["ABPOS"].TableName = "ABPOS_EXT";
                INTEXBL.Save2Xml(SResult, path);
                INTEXBL.AutoConvert(path, "JLTAV95");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                //MessageBox.Show("初始化已经完成，请确定");
                //btnQuery_Click(null, null);
            }
        }
    }
}